# -*- coding:utf-8 -*-

import requests
from lxml import etree
from fake_useragent import UserAgent
from http import cookiejar
import json
from datetime import datetime
import time
import re
import csv
from utils.HttpUtils import HttpUtils


requests.packages.urllib3.disable_warnings()  # 忽略HTTPS安全警告


"""
COVID-19新冠肺炎 丁香医生
https://ncov.dxy.cn/ncovh5/view/pneumonia
"""



class COVID_Get():
    def __init__(self):
        #声明一个CookieJar对象实例来保存cookie
        self.cookie = cookiejar.CookieJar()
        ua = UserAgent(use_cache_server=False)  # 禁用服务器缓存
        self.headers = {
            "User-Agent": ua.random
            # 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.1 Safari/605.1.15',
        }

    # 省份、城市总数
    def get_contents(self):
        url = "https://ncov.dxy.cn/ncovh5/view/pneumonia"
        html = HttpUtils.do_request("GET", url, self.headers, "")
        html.encoding = "utf-8"
        data_js = re.findall(r"window.getAreaStat = (.*?)\}catch", html.text)[0]
        data_js_country = re.findall(r"window.getListByCountryTypeService2true = (.*?)\}catch", html.text)[0]
        data_json_province = json.loads(data_js)
        data_json_country = json.loads(data_js_country)
        # 各个省份
        self.get_province(data_json_province)
        # 各个洲及明细
        # self.get_country(data_json_country)

    # 各个省份
    def get_province(self, data_json_province):
        with open("./COVID-19_省_市.csv", 'a+', encoding="utf-8-sig", newline='') as f:
            csv_writer = csv.writer(f)
            csv_writer.writerow(["省份", "现存确诊", "累计确诊", "累计死亡", "治愈", "province_id", "城市", "现存确诊", "累计确诊",
                                 "累计死亡", "治愈", "city_id", "json"])
        with open("./COVID-19_省_日期.csv", 'a+', encoding="utf-8-sig", newline='') as f:
            csv_writer = csv.writer(f)
            csv_writer.writerow(["省份", "日期", "现确诊", "累计确诊", "新增确诊", "累计死亡", "新增死亡",
                                 "累计治愈", "新增治愈", "累计疑似", "新增疑似"])
        for d in data_json_province:
            dict_data = dict()
            dict_data['provinceName'] = d['provinceName']  # 省份
            dict_data['currentConfirmedCount'] = d['currentConfirmedCount']  # 确诊
            dict_data['confirmedCount'] = d['confirmedCount']  # 累计确诊
            dict_data['deadCount'] = d['deadCount']  # 累计死亡
            dict_data['curedCount'] = d['curedCount']  # 治愈
            dict_data['locationId'] = d['locationId']  # ID
            dict_data['statisticsData'] = d['statisticsData']  # 详细数据json链接
            # 各个城市明细
            for c in d['cities']:
                dict_data['cityName'] = c['cityName']  # 城市
                dict_data['currentConfirmedCount_c'] = c['currentConfirmedCount']  # 现确诊
                dict_data['confirmedCount_c'] = c['confirmedCount']  # 累计确诊
                dict_data['deadCount_c'] = c['deadCount']  # 累计死亡
                dict_data['curedCount_c'] = c['curedCount']  # 治愈
                dict_data['locationId_c'] = c['locationId']  # ID
                print(str(dict_data))
                with open("./COVID-19_省_市.csv", 'a', encoding="utf-8-sig", newline='') as csvfile:
                    fileheader = ["provinceName", "currentConfirmedCount", "confirmedCount", "deadCount", "curedCount",
                                  "locationId", "cityName", "currentConfirmedCount_c", "confirmedCount_c",
                                  "deadCount_c", "curedCount_c", "locationId_c", "statisticsData"]
                    writer = csv.DictWriter(csvfile, fieldnames=fileheader)
                    writer.writerow(dict_data)
            self.province_detail(d['statisticsData'], d['provinceName'])


    # 省份日期数据
    def province_detail(self, url, province):
        html = HttpUtils.do_request("GET", url, self.headers, "")
        data_json = json.loads(html.text)
        for d in data_json['data']:
            dict_data = dict()
            dict_data['province'] = province  # 省份
            dict_data['dateId'] = d['dateId']  # 日期
            dict_data['currentConfirmedCount'] = d['currentConfirmedCount']  # 现存确诊
            dict_data['confirmedCount'] = d['confirmedCount']  # 累计确诊
            dict_data['confirmedIncr'] = d['confirmedIncr']  # 新增确诊
            dict_data['deadCount'] = d['deadCount']  # 累计死亡
            dict_data['deadIncr'] = d['deadIncr']  # 新增死亡
            dict_data['curedCount'] = d['curedCount']  # 累计治愈
            dict_data['curedIncr'] = d['curedIncr']  # 新增治愈
            dict_data['suspectedCount'] = d['suspectedCount']  # 疑似累计
            dict_data['suspectedCountIncr'] = d['suspectedCountIncr']  # 疑似新增
            with open("./COVID-19_省_日期.csv", 'a', encoding="utf-8-sig", newline='') as csvfile:
                fileheader = ["province", "dateId", "currentConfirmedCount", "confirmedCount", "confirmedIncr",
                              "deadCount", "deadIncr", "curedCount", "curedIncr", "suspectedCount", "suspectedCountIncr"]
                writer = csv.DictWriter(csvfile, fieldnames=fileheader)
                writer.writerow(dict_data)
            print(str(dict_data))


    def get_city_detail(self, url):
        html = HttpUtils.do_request("GET", url, self.headers, "")
        data_json = json.loads(html.text)
        for d in data_json['data']:
            dict_data = dict()
            dict_data['dateId'] = d['dateId']  # 日期
            dict_data['currentConfirmedCount'] = d['currentConfirmedCount']  # 现存确诊
            dict_data['confirmedCount'] = d['confirmedCount']  # 累计确诊
            dict_data['confirmedIncr'] = d['confirmedIncr']  # 新增确诊
            dict_data['deadCount'] = d['deadCount']  # 累计死亡
            dict_data['deadIncr'] = d['deadIncr']  # 新增死亡
            dict_data['curedCount'] = d['curedCount']  # 累计治愈
            dict_data['curedIncr'] = d['curedIncr']  # 新增治愈
            dict_data['suspectedCount'] = d['suspectedCount']  # 疑似累计
            dict_data['suspectedCountIncr'] = d['suspectedCountIncr']  # 疑似新增



    # 全球各个州
    def get_country(self, data_json_country):
        # 各个洲详细
        with open("./COVID-19_各个洲详细.csv", 'a+', encoding="utf-8-sig", newline='') as f:
            csv_writer = csv.writer(f)
            csv_writer.writerow(["洲", "国家", "日期", "现确诊", "累计确诊", "新增确诊", "累计死亡", "新增死亡", "累计治愈", "新增治愈"])
        for d in data_json_country:
            dict_data = dict()
            dict_data['continents'] = d['continents']  # 洲
            dict_data['provinceName'] = d['provinceName']  # 省份
            dict_data['currentConfirmedCount'] = d['currentConfirmedCount']  # 确诊
            dict_data['confirmedCount'] = d['confirmedCount']  # 累计确诊
            dict_data['deadCount'] = d['deadCount']  # 累计死亡
            dict_data['curedCount'] = d['curedCount']  # 治愈
            dict_data['locationId'] = d['locationId']  # ID
            dict_data['statisticsData'] = d['statisticsData']  # 详细数据json链接
            print(str(dict_data))
            # 各个洲详细
            html = HttpUtils.do_request("GET", d['statisticsData'],self.headers, "")
            data_json_country_detail = json.loads(html.text)
            for d1 in data_json_country_detail['data']:
                dict_data = dict()
                dict_data['continents'] = d['continents']  # 洲
                dict_data['provinceName'] = d['provinceName']  # 省份
                dict_data['dateId'] = d1['dateId']  # 日期
                dict_data['currentConfirmedCount'] = d1['currentConfirmedCount']  # 现存确诊
                dict_data['confirmedCount'] = d1['confirmedCount']  # 累计确诊
                dict_data['confirmedIncr'] = d1['confirmedIncr']  # 新增确诊
                dict_data['deadCount'] = d1['deadCount']  # 累计死亡
                dict_data['deadIncr'] = d1['deadIncr']  # 新增死亡
                dict_data['curedCount'] = d1['curedCount']  # 累计治愈
                dict_data['curedIncr'] = d1['curedIncr']  # 新增治愈
                with open("./COVID-19_各个洲详细.csv", 'a+', encoding="utf-8-sig", newline='') as csvfile:
                    fileheader = ["continents", "provinceName", "dateId", "currentConfirmedCount",
                                         "confirmedCount", "confirmedIncr", "deadCount", "deadIncr",
                                         "curedCount", "curedIncr"]
                    writer = csv.DictWriter(csvfile, fieldnames=fileheader)
                    writer.writerow(dict_data)

    # 全球总数
    def get_global(self):
        with open("./COVID-19_全球总数.csv", 'a+', encoding="utf-8-sig", newline='') as f:
            csv_writer = csv.writer(f)
            csv_writer.writerow(["日期", "现存确诊", "累计确诊", "新增确诊", "累计死亡", "新增死亡", "累计治愈",
                                 "新增治愈"])
        url = "https://file1.dxycdn.com/2020/0418/233/3408432868014669623-135.json"
        html = HttpUtils.do_request("GET", url, self.headers, "")
        data_json = json.loads(html.text)
        for d in data_json['data']:
            dict_data = dict()
            dict_data['dateId'] = d['dateId']  # 日期
            dict_data['currentConfirmedCount'] = d['currentConfirmedCount']  # 现存确诊
            dict_data['confirmedCount'] = d['confirmedCount']  # 累计确诊
            dict_data['confirmedIncr'] = d['confirmedIncr']  # 新增确诊
            dict_data['deadCount'] = d['deadCount']  # 累计死亡
            dict_data['deadIncr'] = d['deadIncr']  # 新增死亡
            dict_data['curedCount'] = d['curedCount']  # 累计治愈
            dict_data['curedIncr'] = d['curedIncr']  # 新增治愈
            print(str(dict_data))
            with open("./COVID-19_全球总数.csv", 'a', encoding="utf-8-sig", newline='') as csvfile:
                fileheader = ["dateId", "currentConfirmedCount", "confirmedCount", "confirmedIncr",
                              "deadCount", "deadIncr", "curedCount", "curedIncr"]
                writer = csv.DictWriter(csvfile, fieldnames=fileheader)
                writer.writerow(dict_data)


if __name__ == '__main__':
    covid = COVID_Get()

    covid.get_contents()

    # covid.get_global()